package com.example.project.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.project.entity.IssueRelation;

/**
 * 问题关联关系Mapper接口
 * 
 * @author ruoyi
 * @date 2025-11-24
 */
@Mapper
public interface IssueRelationMapper {

	/**
	 * 查询问题关联关系
	 * 
	 * @param id 问题关联关系ID
	 * @return 问题关联关系
	 */
	@Select("SELECT id, issue_from_id, issue_to_id, relation_type, description, create_by, create_time, update_by, update_time "
			+ "FROM t_issue_relation WHERE del_flag = 0 and id = #{id}")
	IssueRelation selectIssueRelationById(Long id);
	

	@Select("SELECT id, issue_from_id, issue_to_id, relation_type, description, create_by, create_time, update_by, update_time "
			+ "FROM t_issue_relation WHERE del_flag = 0 and issue_from_id = #{issueFromId} and issue_to_id = #{issueToId} limit 1")
	IssueRelation findByFromIdAndToId(Long issueFromId,Long issueToId);

	/**
	 * 查询问题关联关系列表
	 * 
	 * @param issueRelation 问题关联关系
	 * @return 问题关联关系集合
	 */
	@Select("<script>"
			+ "SELECT id, issue_from_id, issue_to_id, relation_type, description, create_by, create_time, update_by, update_time "
			+ "FROM t_issue_relation where del_flag = 0 " 
			+ "<if test='issueFromId != null'> AND issue_from_id = #{issueFromId}</if>"
			+ "<if test='issueToId != null'> AND issue_to_id = #{issueToId}</if>"
			+ "<if test='relationType != null and relationType != \"\"'> AND relation_type = #{relationType}</if>"
			+ "</script>")
	List<IssueRelation> selectIssueRelationList(IssueRelation issueRelation);

	/**
	 * 根据问题ID查询关联关系列表
	 * 
	 * @param issueId 问题ID
	 * @return 关联关系列表
	 */
	@Select("SELECT id, issue_from_id, issue_to_id, relation_type, description, create_by, create_time, update_by, update_time "
			+ "FROM t_issue_relation WHERE del_flag = 0 and issue_from_id = #{issueId} ORDER BY create_time DESC")
	List<IssueRelation> selectIssueRelationsByIssueId(Long issueId);

	/**
	 * 新增问题关联关系
	 * 
	 * @param issueRelation 问题关联关系
	 * @return 结果
	 */
	@Insert("<script>" + "INSERT INTO t_issue_relation " + "<trim prefix='(' suffix=')' suffixOverrides=','>"
			+ "<if test='issueFromId != null'>issue_from_id,</if>" + "<if test='issueToId != null'>issue_to_id,</if>"
			+ "<if test='relationType != null and relationType != \"\"'>relation_type,</if>"
			+ "<if test='description != null and description != \"\"'>description,</if>"
			+ "<if test='createBy != null and createBy != \"\"'>create_by,</if>"
			+ "<if test='createTime != null'>create_time,</if>"
			+ "<if test='updateBy != null and updateBy != \"\"'>update_by,</if>"
			+ "<if test='updateTime != null'>update_time,</if>" + "</trim> "
			+ "<trim prefix='VALUES (' suffix=')' suffixOverrides=','>"
			+ "<if test='issueFromId != null'>#{issueFromId},</if>" + "<if test='issueToId != null'>#{issueToId},</if>"
			+ "<if test='relationType != null and relationType != \"\"'>#{relationType},</if>"
			+ "<if test='description != null and description != \"\"'>#{description},</if>"
			+ "<if test='createBy != null and createBy != \"\"'>#{createBy},</if>"
			+ "<if test='createTime != null'>#{createTime},</if>"
			+ "<if test='updateBy != null and updateBy != \"\"'>#{updateBy},</if>"
			+ "<if test='updateTime != null'>#{updateTime},</if>" + "</trim>" + "</script>")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	int insertIssueRelation(IssueRelation issueRelation);

	/**
	 * 修改问题关联关系
	 * 
	 * @param issueRelation 问题关联关系
	 * @return 结果
	 */
	@Update("<script>" + "UPDATE t_issue_relation " + "<trim prefix='SET' suffixOverrides=','>"
			+ "<if test='issueFromId != null'>issue_from_id = #{issueFromId},</if>"
			+ "<if test='issueToId != null'>issue_to_id = #{issueToId},</if>"
			+ "<if test='relationType != null and relationType != \"\"'>relation_type = #{relationType},</if>"
			+ "<if test='description != null and description != \"\"'>description = #{description},</if>"
			+ "<if test='updateBy != null and updateBy != \"\"'>update_by = #{updateBy},</if>"
			+ "<if test='updateTime != null'>update_time = #{updateTime},</if>" + "</trim> " + "WHERE id = #{id}"
			+ "</script>")
	int updateIssueRelation(IssueRelation issueRelation);

	/**
	 * 删除问题关联关系
	 * 
	 * @param id 问题关联关系ID
	 * @return 结果
	 */
//	@Delete("DELETE FROM t_issue_relation WHERE id = #{id}")
	@Update("update t_issue_relation set del_flag = 1, update_time = NOW() where id = #{id}")
	int deleteIssueRelationById(Long id);

}